package com.liuxl.core.util;

import java.util.Date;
import java.util.HashMap;

import com.liuxl.core.common.domain.AjaxResult;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

/**
 * http 网络访问工具
 *
 * @author liuxl.
 * @version 3.0.0
 */
public class HttpBaseHutool  extends AjaxResult{

    /**
	 *	 序列化
	 */
	private static final long serialVersionUID = 1L;

	private static final String URL = "http://61.175.187.6:81";
    
    private static final String  PUBKEY_URL = URL+"/WPMS/getPublicKey?spm=1543396612680";
    private static final String  TOKEN_URL = URL+"/WPMS/login";
    //考勤记录url
    private static final String DURY_URL = URL+"/CardSolution/attendance/result/page";
    //刷卡记录url
    private static final String MARK_URL = URL+"/CardSolution/attendance/record/page";
    //需增加断线续传接口
    private static final String  INSERTBATCH_URL = URL+ "/CardSolution/acs/additionalRecords/insertBatch";
    //断点续传查询结果
    private static final String ACS_URL = URL+ "/CardSolution/acs/additionalRecords/page/condition/third";
    //断点续传设备编号
    private static final String device_Codes = "1000193,1000228,1000278,1000280,1000287,1000293,1000299,1000301,1000303,1000341,1000374,1000423,1000433";
    
    
    public static String getPubkey()  {
    	
    	HashMap<String, Object> paramMap = new HashMap<>();
    	paramMap.put("loginName", "system");
    	String result = HttpRequest.post(PUBKEY_URL)
    		    .header(Header.CONTENT_TYPE, "application/json")//头信息，多个头信息多次调用此方法即可
    		    .body(JSONUtil.parseObj(paramMap))//表单内容
    		    .timeout(20000)//超时，毫秒
    		    .execute().body();
    	JSONObject jsonObject = JSONUtil.parseObj(result);
    	return jsonObject.getStr("publicKey");
    }
    
    
    public static String getToken(String pubkey)  {
    	try {
    		if(pubkey==null) {
        		pubkey = getPubkey();
        	}
    		
        	String rsa = RSAEncrypt.encrypt("admin123",pubkey);
        	HashMap<String, Object> paramMap = new HashMap<>();
        	paramMap.put("loginName", "system");
        	paramMap.put("loginPass", rsa);
        	String result = HttpRequest.post(TOKEN_URL)
        		    .header(Header.CONTENT_TYPE, "application/json")//头信息，多个头信息多次调用此方法即可
        		    //.body("{'loginName':'system','loginPass':'"+rsa+"'}")//表单内容
        		    .body(JSONUtil.parseObj(paramMap))
        		    .timeout(20000)//超时，毫秒
        		    .execute().body();
        	JSONObject jsonObject = JSONUtil.parseObj(result);
        	return jsonObject.getStr("token");
		} catch (Exception e) {
			// TODO: handle exception
			return "500";
		}
    	
    }
    
    public static JSONObject getDuty(String token,HashMap<String,Object> map) {
    	
    	String url = DURY_URL+"?token= "+token;
    	map.put("pageSize", 20);
    	map.put("result", 1);
    	System.out.println(JSONUtil.parseObj(map));
    	String result = HttpRequest.post(url)
    		    .header(Header.CONTENT_TYPE, "application/json")//头信息，多个头信息多次调用此方法即可
    		    .body(JSONUtil.parseObj(map))//表单内容
    		    .timeout(20000)//超时，毫秒
    		    .execute().body();
    	
    	JSONObject jsonObject = JSONUtil.parseObj(result);
    	return jsonObject.getJSONObject("data");
    }
    
    public static String  resumeMark(String token,HashMap<String,Object> map) {
    	
    	String url = INSERTBATCH_URL+"?token= "+token;
    	String today= DateUtil.today();
//    	String today = "2019-12-05";
    	map.put("startTime", today+" 00:00:00");
    	map.put("endTime", today+" 23:59:59");
    	map.put("deviceCodes",device_Codes);
    	
//    	返回值
//    	{
//    	    "success": true,
//    	    "code": null,
//    	    "errMsg": "已生成补采任务，请至补采日志模块查看任务进度！",
//    	    "data": ""
//    	}
    	
    	String result = HttpRequest.post(url)
    		    .header(Header.CONTENT_TYPE, "application/json")//头信息，多个头信息多次调用此方法即可
    		    .body(JSONUtil.parseObj(map))//表单内容
    		    .timeout(20000)//超时，毫秒
    		    .execute().body();
    	
    	JSONObject jsonObject = JSONUtil.parseObj(result);
    	System.out.println("result:::"+jsonObject.get("errMsg"));
    	return jsonObject.getStr("success");
    }
    
    
    
	public static JSONObject getMark(String token,HashMap<String,Object> map) {
	    	
		String url = MARK_URL+"?token= "+token;
		map.put("pageSize", 20);
		String result = HttpRequest.post(url)
			    .header(Header.CONTENT_TYPE, "application/json")//头信息，多个头信息多次调用此方法即可
			    .body(JSONUtil.parseObj(map))//表单内容
			    .timeout(20000)//超时，毫秒
			    .execute().body();
		
		JSONObject jsonObject = JSONUtil.parseObj(result);
		return jsonObject.getJSONObject("data");
	}
	
    public static String  contineResult(String token,HashMap<String,Object> map) {
    	
//    	{
//    		"pageNum": 1,
//    		"pageSize": 20,
//    		"startTimeString": "2019-06-17 00:00:00",
//    		"endTimeString": "2019-06-17 23:59:59"
//    		}
    	String today= DateUtil.today();
//    	map.put("deviceCodes", "");
    	map.put("pageNum", 1);
    	map.put("pageSize", 20);
    	map.put("result", 1); //0:未开始,1:补采中2：补采失败3：补采成功     
    	map.put("startTimeString", today+" 00:00:00");
    	map.put("endTimeString", today+" 23:59:59");
    	
    	String url = ACS_URL+"?token= "+token;
    	String result = HttpRequest.post(url)
    		    .header(Header.CONTENT_TYPE, "application/json")//头信息，多个头信息多次调用此方法即可
    		    .body(JSONUtil.parseObj(map))//表单内容
    		    .timeout(20000)//超时，毫秒
    		    .execute().body();
    	
    	JSONObject jsonObject = JSONUtil.parseObj(result);
    	System.out.println(jsonObject.toString());
    	return jsonObject.getJSONObject("data").getStr("totalRows");
    	
    }
    
    
    
    
    public static void main(String[] args) {
		try {
//			String token = HttpBaseHutool.getToken(null);
			//发送指令
//			Object o = resumeMark(token,new HashMap<String,Object>());
//			JSONObject jsonObject = JSONUtil.parseObj(o);
//	    	System.out.println(jsonObject);
			
			
//	    	Object o = contineResult(token,new HashMap<String,Object>());
//	    	JSONObject jsonObject = JSONUtil.parseObj(o);
//	    	System.out.println(o.toString());
	    	HashMap<String, Object> paramMap = new HashMap<>();
	    	//文件上传只需将参数中的键指定（默认file），值设为文件对象即可，对于使用者来说，文件上传与普通表单提交并无区别
	    	paramMap.put("file", FileUtil.file("D:\\log\\lame.log"));  //file 对应前端的name值

	    	String result= HttpUtil.post("http://localhost:8081/upliu/upfile/1", paramMap);
	    	System.out.println(result);
	    	
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	//RSA rsa = SecureUtil.rsa("admin123", "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCju1AxCYY1iDdTCuisGquT5R624Ol0sJptLKd9VSbBymt5rYOU9cBCH7MMkV1lZFojt6XTDWFeqa4h4PaMD3YZBTRRdTH3ITWLDvL5zYR/k7gyaWmxz3hXM0VkEvinnNLJGHvLLSOI7mKX2XlIm44OrITiJr8VG8FvDwGuTzWXFQIDAQAB");
//    	System.out.println(rsa.toString());
    	
	}
}
